//
//  UILabel+AttriLabel.m
//  丁诚昊的外包项目(2016_V7宝)
//
//  Created by DimChum_Haul on 16/3/29.
//  Copyright © 2016年 沙罗双树831. All rights reserved.
//

#import "UILabel+AttriLabel.h"
#import "UIColor+ColorFullTemplate.h"

@interface UILabel ()
@property(assign,nonatomic,readwrite) CGSize calcRectByString;
@end

@implementation UILabel (AttriLabel)
-(instancetype) initWithTitle:(NSString*)title
                     withFont:(UIFont*)fontNumber
                   thinBorder:(BOOL)Thin
{
    if(Thin) {
        self = [self initWithTitle:title withFont:fontNumber backColor:nil];
        self.layer.cornerRadius = 0;
        self.clipsToBounds = self.layer.cornerRadius;
        self.layer.borderColor = [UIColor blackColor].CGColor;
        self.layer.borderWidth = 1.0f;
        CGSize size = [title sizeWithAttributes:@{NSFontAttributeName:fontNumber}];
        CGSize resultSize = CGSizeMake(size.width*1.5f, size.height*1.2f);
        self.calcRectByString = resultSize;
        
        return self;
    }else {
        return [self initWithTitle:title withFont:fontNumber backColor:nil];
    }
    return nil;
}

-(instancetype) initWithTitle:(NSString*)title
                     withFont:(UIFont*)fontNumber
                    backColor:(UIColor*)backColor
{
    if(self = [super init]){
        
        self.font = fontNumber;
        self.textAlignment = NSTextAlignmentCenter;
        
        if(backColor == nil){
            self.text = title;
            self.textColor = [UIColor whiteColor];
        }else{
            self.attributedText = [NSAttributedString AttriStringByLengthOfChar:title.length
                                                                      oriString:title];
        }
        self.numberOfLines = INTMAX_MAX;
        
        [self adjustsFontSizeToFitWidth];
        self.backgroundColor = (backColor==nil)?[UIColor Heavy_ColorfulDay]:backColor;
        self.layer.borderWidth = 3.3f;
        self.layer.borderColor = [UIColor Heavy_ColorfulDay].CGColor;
        self.layer.cornerRadius = 10.0f;
        self.clipsToBounds = YES;
        
        ///设置阴影
        self.layer.shadowColor = [UIColor grayGrayColor].CGColor;
        self.layer.shadowOffset = CGSizeMake(5, 5);
        self.layer.shadowRadius = 5.0f;
        self.layer.shadowOpacity = 1.0f;
        
        CGSize size = [title sizeWithAttributes:@{NSFontAttributeName:fontNumber}];
        CGSize resultSize = CGSizeMake(size.width*1.1f, size.height*2.0f);
        self.calcRectByString = resultSize;
        return self;
    }
    return nil;
}

- (CGSize)calcSize{
    return self.calcRectByString;
}
@end


@implementation NSAttributedString(AttriLabel)
+(NSAttributedString*) AttriStringByLengthOfChar:(unsigned long)SubRangeCount
                                       oriString:(NSString*)title
{
    if(SubRangeCount > title.length)
    {
        NSLog(@"字符串错误--->索引越界");
        return nil;
    }
    
    NSMutableAttributedString* attriString = [[NSMutableAttributedString alloc]initWithString:title attributes:nil];
    for (int i = 0; i < attriString.length; i++)
    {
        [attriString addAttribute:NSForegroundColorAttributeName
                            value:[UIColor Heavy_ColorfulDay]
                            range:NSMakeRange(i, 1)];
        [attriString addAttribute:NSBackgroundColorAttributeName
                            value:[UIColor clearColor]
                            range:NSMakeRange(i, 1)];
    }
    return attriString;
}
@end